package com.mini.gateway.web.handler;

import com.mini.gateway.plugin.exception.BusinessException;
import com.mini.gateway.web.domain.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ControllerHandler
 *
 */
@ControllerAdvice
public class ControllerHandler {

    private static final Logger logger = LoggerFactory.getLogger(ControllerHandler.class);

    /** request存储key */
    private static final String HTTP_REQUEST = "http_request";
    /** 请求地址 */
    private static final String CONTEXT_PATH = "context_path";


    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public JsonResult exceptionHandler(BusinessException ex) {
        logger.error(ex.getMessage(), ex);
        return JsonResult.error(ex.getMessage());
    }

    /**
     * 异常处理
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResult exceptionHandler(Exception ex) {
        logger.error(ex.getMessage(), ex);
        return JsonResult.error("服务不稳定");
    }
}
